package com.zjut.zs.pay.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.zjut.an.util.module.PageModule;
import com.zjut.an.util.tools.JSONUtilClass;
import com.zjut.an.util.tools.JSONUtilClassTime;
import com.zjut.zs.excel.tools.ExcelUtils;
import com.zjut.zs.excel.tools.JsGridReportBase;
import com.zjut.zs.excel.tools.TableData;
import com.zjut.zs.pay.dao.ZsPay;
import com.zjut.zs.pay.manager.ZsPayManager;

public class ZsPayAction extends ActionSupport implements
ServletResponseAware, ServletRequestAware, ModelDriven<ZsPay>{

	private HttpServletRequest request;
	private HttpServletResponse response;
	private HttpSession session;
	
	private ZsPay obj;
	private Map<String , Object> dataMap;
	private int page = 1, limit = 10;
	private String order;
	private String sort;
	private String search;
	private String rowstr;
	
	@Resource(name = "ZsPayManager")
	private ZsPayManager manager;
	
	public String zsPayList() {
		try {
			if (session.getAttribute("admin")!=null) {//只有管理员才允许执行该操作，以免信息泄露
				request.setCharacterEncoding("UTF-8");
				dataMap = new HashMap<String, Object>();
				if(search!=null) {
					obj = new ZsPay();
					obj.setUsName(search);
					search = null;
				}
				PageModule module = manager.getDataByInstance(page, limit, sort, order, obj);
				dataMap.put("rows", module.getObjects());
				dataMap.put("total", module.getTotal()); 
				obj = null; 
			}			
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}		
	}
	
	@SuppressWarnings("unchecked")
	public String zsPayDeletes() {
		try {
			String objumniJSON = rowstr;
			manager.deleteDatas(JSONUtilClass.getDTOList(objumniJSON, ZsPay.class));
			dataMap = new HashMap<String, Object>();
			dataMap.put("flag", "success");
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}		
	}
	
	public String zsPayAdd() {
		try {
			String objumniJSON = rowstr;
			obj = (ZsPay)JSONUtilClass.getDTO(objumniJSON, ZsPay.class);
			manager.addData(obj);
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}
	
	public String zsPayDelete() {
		try {
			String objumniJSON = rowstr;
			manager.deleteData((ZsPay)JSONUtilClass.getDTO(objumniJSON, ZsPay.class));
			dataMap = new HashMap<String, Object>();
			dataMap.put("flag", "success");
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}		
	}
	
	public String zsPayUpdate() {
		try {
			String objumniJSON = rowstr;
			manager.updateData((ZsPay)JSONUtilClass.getDTO(objumniJSON, ZsPay.class));
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}		
	}
	
	public String zsPaySave() {
		obj = (ZsPay)(JSONUtilClass.getDTO(rowstr, ZsPay.class));
		if (obj.getPayId()!=null) {
			//-------update-------
			manager.updateData(obj);
		} else {
			//-------insert-------
			manager.addData(obj);
		}
		rowstr = null;
		return SUCCESS;
	}

	public String zsPay() {
		try {
			request.setCharacterEncoding("UTF-8");
			dataMap = new HashMap<String, Object>();
			Integer inId = Integer.parseInt(request.getParameter("inId"));
			obj = manager.getData(inId);
			dataMap.put("rows", obj);
			obj = null;
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}		
	}
	
	public void zsPayExportExcel() throws Exception {
		try {
			if (session.getAttribute("admin")!=null) {//只有管理员才允许执行该操作，以免信息泄露
				request.setCharacterEncoding("UTF-8");
		        response.setContentType("application/msexcel;charset=utf-8");
		        
		        List list = manager.getDataByInstance(1, 10000, sort, order, obj).getObjects();
		        String title = "缴费记录";
		        String[] hearders = {"id","用户ID","身份证","姓名","本地订单号","支付编号","项目类型","支付渠道","下单时间","支付时间","金额","状态"};
	            String[] fields = {"payId","usId","usIdcard","usName","outTradeNo","transactionId","payType","payWay","orderTime","timeEnd","totalFee","payEnd"};
	                
	            TableData td = this.creatTableData(list, hearders, fields);
	            JsGridReportBase report = new JsGridReportBase(request, response);
	            report.exportToExcel(title, "admin", td, false);
			}
			
		}catch(RuntimeException e) {
			e.printStackTrace();
		}
	}
	
	public void zsPayExportExcelBySelect() throws Exception {
		try {
			if (session.getAttribute("admin")!=null) {//只有管理员才允许执行该操作，以免信息泄露
				request.setCharacterEncoding("UTF-8");
		        response.setContentType("application/msexcel;charset=utf-8");
		        
		        String objumniJSON = rowstr;
		        System.out.println("######################" +rowstr);
				List<ZsPay> list = JSONUtilClassTime.getDTOList(objumniJSON, ZsPay.class);
				
		        String title = "缴费记录";
		        String[] hearders = {"id","用户ID","身份证","姓名","本地订单号","支付编号","项目类型","支付渠道","下单时间","支付时间","金额","状态"};
	            String[] fields = {"payId","usId","usIdcard","usName","outTradeNo","transactionId","payType","payWay","orderTime","timeEnd","totalFee","payEnd"};
	             
	            TableData td = this.creatTableData(list, hearders, fields);
	            JsGridReportBase report = new JsGridReportBase(request, response);
	            report.exportToExcel(title, "admin", td, false);
			}			
		}catch(RuntimeException e) {
			e.printStackTrace();
		}
	}
	
	private TableData creatTableData(List list, String[] hearders, String[] fields) {
		TableData td = ExcelUtils.createTableData(list, ExcelUtils.createTableHeader(hearders),fields);
		return td;
	}

	@Override
	public ZsPay getModel() {
		if (null == obj) {
			obj = new ZsPay();
		}
		return obj;
	}

	@Override
	public void setServletRequest(HttpServletRequest arg0) {
		request = arg0;
		session = request.getSession();
	}

	@Override
	public void setServletResponse(HttpServletResponse arg0) {
		response = arg0;
	}

	public HttpServletRequest getRequest() {
		return request;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	public HttpServletResponse getResponse() {
		return response;
	}

	public void setResponse(HttpServletResponse response) {
		this.response = response;
	}

	public HttpSession getSession() {
		return session;
	}

	public void setSession(HttpSession session) {
		this.session = session;
	}

	public ZsPay getAl() {
		return obj;
	}

	public void setAl(ZsPay obj) {
		this.obj = obj;
	}

	public Map<String, Object> getDataMap() {
		return dataMap;
	}

	public void setDataMap(Map<String, Object> dataMap) {
		this.dataMap = dataMap;
	}

	public String getRowstr() {
		return rowstr;
	}

	public void setRowstr(String rowstr) {
		this.rowstr = rowstr;
	}

	public ZsPayManager getManager() {
		return manager;
	}

	public void setManager(ZsPayManager manager) {
		this.manager = manager;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public String getOrder() {
		return order;
	}

	public void setOrder(String order) {
		this.order = order;
	}

	public String getSort() {
		return sort;
	}

	public void setSort(String sort) {
		this.sort = sort;
	}

	public String getSearch() {
		return search;
	}

	public void setSearch(String search) {
		this.search = search;
	}

}
